<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Mail;
use Hash;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //前台的登录页面
    public function getIndex()
    {
       return view('/home/login');
    }
    //ajax  注册
    public function postAjax(Request $request)
    {
    	$username= $request->input('username');
    	$res = DB::table('zheye_user')->where('username',$username)->first();
    	if($res) {
    		echo 0;
    	} else {
    		echo 1;
    	}
    	
    }   
   // 邮箱发送验证码
    public function postYzfs(Request $request)
    {
    	   $email = $request->input('email');
    	   $data = ['email'=>$email];
    	   $rand = rand(100009,999999);
    	   mail::raw($rand,function($m) use($data){
					$m->subject('提醒激活邮件');
					$m->to($data['email']);
			});
           Session::flash('vcode',$rand);
           echo 1;
    }

    public function postYzpp(Request $request)
    {
        $yzm = $request->input('yzm');
        // var_dump($yzm);
        $password = $request->input('password');
        $res = $request->only('email','username','password');
        $res['password'] = Hash::make($password);
        $time = time();
        $res['time']= $time;
        // echo session('vcode');
    	if ($yzm == session('vcode')) {
            DB::table('zheye_user')->insert($res);
            echo 1;
            
       } else {
           // DB::insert("insert into zheye_user(username,password,email) value(".$username.",".$password.",".$email.")");
            echo '验证码错误';
    	}
        
    	
    }






    //判断用户帐号
     public function postDlajax(Request $request)
    {
        // echo 1121;
        $users= $request->input('users');
       // var_dump($users);
        $res = DB::table('zheye_user')->where('username',$users)->first();
        if($res) {
            echo 1;
        } else {
            echo 0;
        }
        
    }   

     public function postSubmit(Request $request)
    {
        $arr = $request->only('user','password');
        $password = $request->input('password');
        $username = $request->input('user');
        $res = DB::table('zheye_user')->where('username',$arr['user'])->first();

        if(Hash::check($password,$res->password)){
            
            if($res->status==1) {
                session(['user_id'=>$res->id]);
                session(['user_user'=>$res->username]);
                session(['user_status'=>$res->status]);
                echo 1;

            } elseif ($res->status==2) {
                session(['user_id'=>$res->id]);
                session(['user_user'=>$res->username]);
                session(['user_status'=>$res->status]);
                echo 2;
                
            }elseif ($res->status==0) {
                echo 3;
            }
        }else{
            echo 0;
        }
  
    }   

     public function getErjitiao()
        {
            echo "12313";
        }
}
